﻿using System;
using Microsoft.Xna.Framework;
using SomeGame.GUI.Widgets.Containers;

namespace SomeGame.GUI.Screens {

    public class BackgroundScreen : Screen {

        private const int NUM_SQUARES = 1000;
        private const int MIN_SPEED = -100; // Pixels per second.
        private const int MAX_SPEED = 100; // Pixels per second.
        private const int RECTANGLE_SIDE_LENGTH = 10;
        private readonly Vector2[] Positions;
        private readonly Vector2[] Velocities;

        public BackgroundScreen() {
            Name = ScreenManager.BACKGROUND_SCREEN;
            RootContainer = new RootContainer(this, GameEngine.Blank, GameEngine.GameBounds, new Color(20, 20, 20, 20));
            RootContainer.Layout();

            //=====( Begin background particles )=====
            Random random = new Random();
            Positions = new Vector2[NUM_SQUARES];
            Velocities = new Vector2[NUM_SQUARES];
            Color[] colors = new Color[NUM_SQUARES];
            for (int i = 0; i < NUM_SQUARES; i++) {
                Positions[i] =
                    new Vector2(random.Next(RootContainer.Bounds.Left, RootContainer.Bounds.Right-RECTANGLE_SIDE_LENGTH),
                        random.Next(RootContainer.Bounds.Top, RootContainer.Bounds.Bottom-RECTANGLE_SIDE_LENGTH));
                Velocities[i] = new Vector2(random.Next(MIN_SPEED, MAX_SPEED), random.Next(MIN_SPEED, MAX_SPEED));
                colors[i] = new Color(random.Next(10, 100), random.Next(10, 100), random.Next(10, 100));
            }
            //=====( End background particles )=====
        }

        public override void Update(GameTime gameTime) {
            //=====( Begin background particles )=====
            for (int i = 0; i < NUM_SQUARES; i++) {
                Vector2 position = Positions[i];
                Vector2 velocity = Velocities[i];
                position = new Vector2(position.X+(velocity.X*gameTime.ElapsedGameTime.Ticks/TimeSpan.TicksPerSecond),
                    position.Y+(velocity.Y*gameTime.ElapsedGameTime.Ticks/TimeSpan.TicksPerSecond));
                if (position.X < 0 && velocity.X < 0) {
                    velocity = new Vector2(velocity.X*-1, velocity.Y);
                } else if (position.X > RootContainer.Bounds.Right-RECTANGLE_SIDE_LENGTH && velocity.X > 0) {
                    velocity = new Vector2(velocity.X*-1, velocity.Y);
                } else if (position.Y < RootContainer.Bounds.Top && velocity.Y < 0) {
                    velocity = new Vector2(velocity.X, velocity.Y*-1);
                } else if (position.Y > RootContainer.Bounds.Bottom-RECTANGLE_SIDE_LENGTH && velocity.Y > 0) {
                    velocity = new Vector2(velocity.X, velocity.Y*-1);
                }
                Positions[i] = position;
                Velocities[i] = velocity;
            }
            //=====( End background particles )=====
            base.Update(gameTime);
        }

        protected override void DrawGameObjects() {
            for (int i = 0; i < NUM_SQUARES; i++) {
                Vector2 position = Positions[i];
                //GameEngine.SpriteBatch.Draw(GameEngine.Blank, new Rectangle((int)position.X, (int)position.Y, RectangleSideLength, RectangleSideLength), Colors[i]);
            }
        }

    }

}